package com.pantuo.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.util.URIUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.SocketTimeoutException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.util.URIUtil;;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.SocketTimeoutException;
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 炳秦
 * @Date: 2021/08/02/13:59
 * @Description:
 */
@Slf4j
public class HttpClientPool {

    static HttpClient httpClient = null;
    public static synchronized HttpClient getHttpClient(int defaultConnectionTimeout, int soTimeout) {
        if (httpClient == null) {
            httpClient =  new HttpClient();
            final MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
            connectionManager.setMaxTotalConnections (3);//总的连接数
            connectionManager.setMaxConnectionsPerHost (80);//每个host的最大连接数
            httpClient.setHttpConnectionManager( connectionManager );
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(defaultConnectionTimeout);
            httpClient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);
            httpClient.getParams().setParameter("http.protocol.cookie-policy", org.apache.commons.httpclient.cookie.CookiePolicy.BROWSER_COMPATIBILITY);
        }
        return httpClient;
    }


    public static final int DEFAULT_CONNECTIONTIMEOUT = 2000;
    public static final int DEFAULT_SOTIMEOUT = 2000;

    public   String doGet(String url, String queryString)  {
        return doGet(url, queryString, null, DEFAULT_CONNECTIONTIMEOUT, DEFAULT_SOTIMEOUT, false);
    }

    public   String doGet(String url, String queryString, String charset, int soTimeout) {
        return doGet(url, queryString, charset, DEFAULT_CONNECTIONTIMEOUT, soTimeout, false);
    }

    //更具地址获取JSON String字符窜
    public static String doGet(String url, String queryString, String charset, int defaultConnectionTimeout, int soTimeout, boolean needResult) {
        String response = null;
        HttpMethod method = null;
        HttpClient client  = null;
        Reader reader = null;
        try {
            client =  HttpClientPool.getHttpClient(defaultConnectionTimeout,soTimeout);//    HttpClientPool.getHttpClient()// new HttpClient();
            method = new GetMethod(url);

            if (null != queryString)
                method.setQueryString(URIUtil.encodeQuery(queryString));
            client.executeMethod(method);
            if (method.getStatusCode() == HttpStatus.SC_OK) {
                if (needResult) {
                    reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
                    response = IOTools.readerToString(reader);
                }
            }
        } catch (URIException e) {
            log.error("  + queryString +   ", e);
        } catch (IOException e) {
            log.error("Url Address" + url + " error", e);
            if (e instanceof SocketTimeoutException) {
                throw new RuntimeException("网络超时");
            }
        } catch (Exception e) {
            log.error("ex:{}", e);
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }
        return response;
    }
}





